function backgroundImage = extractBackground(videoObj)
    % Estimates the background as the incremental average image
    
    nFrames = videoObj.NumberOfFrames;

    % Reads the first frame
    fprintf('\nReading the first frame...');
        first = rgb2gray(read(videoObj, 1));
        backgroundImage = first;
    fprintf('Done!\n');

    fprintf('\nCalculating the incremental average image...');
        for i = 1:nFrames
            a = ((i - 1)/i) * backgroundImage;
            backgroundImage = imadd(a,(1 / i) * rgb2gray(read(videoObj, i)));
        end;
    fprintf('Done!\n');
end

